#!/bin/perl -w

# yang.jin@ebay.com 

use strict;

########### begin of MAIN #############

my $DEFAULTCFG = "/etc/named/named.conf";

my $formatted = "y";
my @conf= ();
my $argnum;

foreach $argnum (0 .. $#ARGV) {
	$_ = $ARGV[$argnum];
	chomp;
	if ( /^-T$/ ) { $formatted = "n"; }
	else { @conf = (@conf, $_ ); }
}

if ( $#conf == -1 ) {
	my @lines = fgetlines("$DEFAULTCFG");
	foreach (@lines) { if ( /^\s*include\s+\"(.*)\"\s*\;/ ) { @conf = (@conf, "$1"); } }
}

read_configs(@conf);

########### end of MAIN #############


sub read_configs {

my @zonecfg = @_;

my $zone;
my $type;
my $masters;
my $file;
my $pip;
my @pips;


  foreach (@zonecfg) {

    my $zones = $_;
    my @lines = fgetlines("$zones");

    my $zonestatement= "";

    foreach (@lines) {
	chomp;
	$_ =~ s/(\/\/|#).*//;   # remove the notes headed with "//" or "#"
	$_ =~ s/\/\*.*\*\///;	# remove the notes in the pair of "/*" and "*/"
	$_ =~ s/\s+/ /g;
 	$_ =~ s/ *allow\-transfer *\{(.*)\} *\;//;
 	$_ =~ s/ *allow\-update *\{(.*)\} *\;//;
 	$_ =~ s/ *allow\-query *\{(.*)\} *\;//;
	$_ =~ s/ *also notify [\d\.\w]+ *\;//;
	$_ =~ s/ *notify [\d\.\w]+ *\;//;
	$_ =~ s/ *check-names [\d\.\w]+ *\;//;
	$_ =~ s/\" in \{/\" \{/i;  # remove "in" following the zone name

	if ( /^\s+$/ ) { $_ = ""; next; }
	
	if ( /^\s*zone\s/ ) {
 		if ( $zonestatement ) { print STDERR "\nThe following zone on $zones cannot be analayzed:\n$zonestatement\n\n"; $zonestatement = $_; }
		else { $zonestatement = "$zonestatement" . "$_"; }
	} 
	else {
		if ( $zonestatement ) { $zonestatement = "$zonestatement" . "$_"; }
		else  { next; }
	}
	
	$_ = $zonestatement; 

	if ( / *zone \"(.*)\" \{ *type slave *\; *masters \{(.*)\} *\; *file \"(.*)\"\ *; *\} *\;/ ) {
		$type = "slave";
		$zone = $1;
		$masters = $2;
		$file = $3;
		$masters =~ s/\s+//g;
		$masters =~ s/\;$//;
		$masters =~ s/\;/\//g;
		
# 		$_ = $zone;
# 		if ( /\.in\-addr\.arpa$/ ) { 
# 			$pip = $_;
# 			$pip =~ s/\.in\-addr\.arpa$//;
# 			# @pips = split(/\./, $pip);
# 			# $pip = join(".", reverse(@pips));
# 			$pip = join(".", reverse split(/\./, $pip));
# 			$file = "named.rev-" . $pip;
# 		}
# 		else { $file = "db." . $zone; }
 		
		$_ = $zonestatement = "";
		if ( $formatted eq "y" ) { printf STDOUT "%-49s %-8s %-32s %-54s\n", $zone, $type, $masters, $file; }
		else { printf STDOUT "%s %s %s %s\n", $zone, $type, $masters, $file; }
	}
	if ( / *zone \"(.*)\" \{ *type slave *\; *file \"(.*)\" *\; *masters \{(.*)\} *\; *\} *\;/ ) { 
		$type = "slave";
		$zone = $1;
		$masters = $3;
		$file = $2;
		$masters =~ s/\s+//g;
		$masters =~ s/\;$//;
		$masters =~ s/\;/\//g;
		
# 		$_ = $zone;
# 		if ( /\.in\-addr\.arpa$/ ) { 
# 			$pip = $_;
# 			$pip =~ s/\.in\-addr\.arpa$//;
# 			# @pips = split(/\./, $pip);
# 			# $pip = join(".", reverse(@pips));
# 			$pip = join(".", reverse split(/\./, $pip));
# 			$file = "named.rev-" . $pip;
# 		}
# 		else { $file = "db." . $zone; }
# 
		$_ = $zonestatement = "";
		if ( $formatted eq "y" ) { printf STDOUT "%-49s %-8s %-32s %-54s\n", $zone, $type, $masters, $file; }
		else { printf STDOUT "%s %s %s %s\n", $zone, $type, $masters, $file; }
	}
	if ( / *zone \"(.*)\" \{ *type stub *\; *masters \{(.*)\} *\; *file \"(.*)\" *\; *\} *\;/ ) {
		$type = "stub";
		$zone = $1;
		$masters = $2;
		$file = $3;
		$masters =~ s/\s+//g;
		$masters =~ s/\;$//;
		$masters =~ s/\;/\//g;
		
# 		$_ = $zone;
# 		if ( /\.in\-addr\.arpa$/ ) { 
# 			$pip = $_;
# 			$pip =~ s/\.in\-addr\.arpa$//;
# 			# @pips = split(/\./, $pip);
# 			# $pip = join(".", reverse(@pips));
# 			$pip = join(".", reverse split(/\./, $pip));
# 			$file = "named.rev-" . $pip;
# 		}
# 		else { $file = "db." . $zone; }
# 		
		$_ = $zonestatement = "";
		if ( $formatted eq "y" ) { printf STDOUT "%-49s %-8s %-32s %-54s\n", $zone, $type, $masters, $file; }
		else { printf STDOUT "%s %s %s %s\n", $zone, $type, $masters, $file; }
	}
	if ( / *zone \"(.*)\" \{ *type stub *\; *file \"(.*)\" *\; *masters \{(.*)\} *\; *\} *\;/ ) { 
		$type = "stub";
		$zone = $1;
		$masters = $3;
		$file = $2;
		$masters =~ s/\s+//g;
		$masters =~ s/\;$//;
		$masters =~ s/\;/\//g;
		
# 		$_ = $zone;
# 		if ( /\.in\-addr\.arpa$/ ) { 
# 			$pip = $_;
# 			$pip =~ s/\.in\-addr\.arpa$//;
# 			# @pips = split(/\./, $pip);
# 			# $pip = join(".", reverse(@pips));
# 			$pip = join(".", reverse split(/\./, $pip));
# 			$file = "named.rev-" . $pip;
# 		}
# 		else { $file = "db." . $zone; }
# 
		$_ = $zonestatement = "";
		if ( $formatted eq "y" ) { printf STDOUT "%-49s %-8s %-32s %-54s\n", $zone, $type, $masters, $file; }
		else { printf STDOUT "%s %s %s %s\n", $zone, $type, $masters, $file; }
	}
	elsif ( / *zone \"(.*)\" \{ *type forward *\; *forwarders \{(.*)\} *\; *\} *\;/ ) {
		$type = "forward";
		$zone = $1;
		$masters = $2;
		$file = "\-";
		$masters =~ s/\s+//g;
		$masters =~ s/\;$//;
		$masters =~ s/\;/\//g;
		$_ = $zonestatement = "";
		if ( $formatted eq "y" ) { printf STDOUT "%-49s %-8s %-32s %-54s\n", $zone, $type, $masters, $file; }
		else { printf STDOUT "%s %s %s %s\n", $zone, $type, $masters, $file; }
	}
	elsif ( / *zone \"(.*)\" \{ *type hint *\; *file \"(.*)\" *\; *\} *\;/ ) {
		$type = "hint";
		$zone = $1;
		$masters = "localhost";
		$file = $2;
		$_ = $zonestatement = "";
		if ( $formatted eq "y" ) { printf STDOUT "%-49s %-8s %-32s %-54s\n", $zone, $type, $masters, $file; }
		else { printf STDOUT "%s %s %s %s\n", $zone, $type, $masters, $file; }
	}
	elsif ( / *zone \"(.*)\" \{ *type master *\; *file \"(.*)\" *\; *\} *\;/ ) {
		$type = "master";
		$zone = $1;
		$masters = "localhost";
		$file = $2;
		$_ = $zonestatement = "";
		if ( $formatted eq "y" ) { printf STDOUT "%-49s %-8s %-32s %-54s\n", $zone, $type, $masters, $file; }
		else { printf STDOUT "%s %s %s %s\n", $zone, $type, $masters, $file; }
	}
	else { 
		next;
	}
    } 
  } 

} 

sub fgetlines {
	my ($file) = @_;
	my @data = ();

	open(FILE,"< $file") || die "\nOpening the file \"$file\" failed.\n\n";
	@data = <FILE>;
	close(FILE);
	return @data;
}
